{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# GraphQL is the new ORM.\n",
    "[REST](https://en.wikipedia.org/wiki/Representational_state_transfer) and [ORMs](https://en.wikipedia.org/wiki/Object-relational_mapping) are both infamous for:\n",
    "* over-fetching: fetching more data than is needed per request\n",
    "* under-fetching: fetching less data than is needed, requiring multiple requests\n",
    "* select N+1 problem: under-fetching applied to multiple associated objects\n",
    "\n",
    "[GraphQL](https://en.wikipedia.org/wiki/GraphQL) aims to overcome REST's shortcomings through a flexible query language, and succeeds in doing so on the client side.  But on the server side, GraphQL resolvers have effectively recreated the same over- and under- fetching problems that have long plagued ORMs.  The fact that ORMs remain popular despite of their inefficiency is a testament to the benefits of having in-memory objects behave consistently.  There is no such trade-off for server-side GraphQL, where the only point of the objects is to be immediately serialized.\n",
    "\n",
    "The so-called [N+1 problem](https://engineering.shopify.com/blogs/engineering/solving-the-n-1-problem-for-graphql-through-batching) is generally acknowledged in the GraphQL community, but this article will argue only the symptoms are being addressed with workarounds like [dataloader](https://github.com/graphql/dataloader).\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "The problems can be seen immediately in GraphQL's own introductory [resolver example](https://graphql.org/learn/execution/#root-fields-resolvers).\n",
    "\n",
    "```javascript\n",
    "Query: {\n",
    "  human(obj, args, context, info) {\n",
    "    return context.db.loadHumanByID(args.id).then(\n",
    "      userData => new Human(userData)\n",
    "    )\n",
    "  }\n",
    "}\n",
    "\n",
    "Human: {\n",
    "  name(obj, args, context, info) {\n",
    "    return obj.name\n",
    "  }\n",
    "}\n",
    "```\n",
    "\n",
    "What makes the `name` resolver trivial?  It's pre-fetched by `loadHumanByID`, whose only parameter is `id`, so it's clearly unaware of whether `name` has been requested.  What if the requested field was a nested object, or a json field, or just a large text blob?  Then one would clearly be directed towards using a non-trivial resolver which fetches the field on demand.  Of course, but then whatever work is common in the human `id` lookup is duplicated.\n",
    "\n",
    "This is by no means specific to SQL or relational databases, but SQL is a convenient lingua franca of databases to demonstrate the inefficiency.  The choices are:\n",
    "* `SELECT * FROM human WHERE id = ?`\n",
    "* `SELECT field FROM human WHERE id = ?` repeated for each \"expensive\" field\n",
    "\n",
    "Even in the simplest possible example, over-fetching has already occurred, and the only proposed workaround is under-fetching.  The single query we want is:\n",
    "* `SELECT f1, f2, ... FROM human WHERE id = ?` for requested fields\n",
    "\n",
    "In other words, exactly what happens with ORMs, except even ORMs typically offer an option of requesting a subset of fields.  Naturally the problem gets worse with list fields.\n",
    "\n",
    "```javascript\n",
    "Human: {\n",
    "  appearsIn(obj) {\n",
    "    return obj.appearsIn // returns [ 4, 5, 6 ]\n",
    "  }\n",
    "}\n",
    "\n",
    "Human: {\n",
    "  starships(obj, args, context, info) {\n",
    "    return obj.starshipIDs.map(\n",
    "      id => context.db.loadStarshipByID(id).then(\n",
    "        shipData => new Starship(shipData)\n",
    "      )\n",
    "    )\n",
    "  }\n",
    "}\n",
    "```\n",
    "\n",
    "Now there are two sets of associate keys (`.appearsIn` and `.starshipIDs`) that have been over-fetched.  Nonetheless `starships` is  under-fetched as well.  The `starships` resolver is neither the most efficient nor the simplest way of resolving this field:\n",
    "* fetch all the data by human `id` in the `starships` resolver\n",
    "* fetch all the data in bulk by `starshipIDs`\n",
    "* push the resolution down to the `Starship` layer if forced to fetch one at a time\n",
    "\n",
    "The example implementation seems to be going out of its way to showcase JavaScript promises.  And the assumptions being made about the underlying data store are unusual:\n",
    "\n",
    "1. The data is relational in nature.\n",
    "1. Associative keys have neglible cost to pre-fetch.\n",
    "1. But joins are not available.\n",
    "1. And neither are `primary key in` queries.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Workaround\n",
    "From GraphQL's [best practices](https://graphql.org/learn/best-practices/#server-side-batching-caching)\n",
    "\n",
    "> GraphQL is designed in a way that allows you to write clean code on the server, where every field on every type has a focused single-purpose function for resolving that value. However without additional consideration, a naive GraphQL service could be very \"chatty\" or repeatedly load data from your databases.\n",
    "\n",
    "> This is commonly solved by a batching technique, where multiple requests for data from a backend are collected over a short period of time and then dispatched in a single request to an underlying database or microservice by using a tool like Facebook's DataLoader.\n",
    "\n",
    "That's an understatement.  It's not clear how \"naive\" differs from best practice.\n",
    "\n",
    "Clearly there is value in transforming multiple `primary key =` queries into a single `primary key in` query.  As in the `starships` example however, that can be done more simply in the parent resolver.  There is more value in not needing a `primary key in` query at all.  Furthermore adding caching to a dataloader avoids the central issue.\n",
    "\n",
    "Again reminiscent of ORMs, any data layer can add caching.  The point is efficiently resolving a query requires context, which strict adherence to single-purpose resolvers explictly disregards."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Aggregation\n",
    "The inefficencies becomes even more glaring when moving beyond associative keys.  Nearly any aggregation requires knowing what summaries are requested.  Such as if the `appearsIn` field optionally included counts or times.  Using SQL as an example again, the query would resemble one of:\n",
    "* `SELECT distinct field FROM ...`\n",
    "* `SELECT field, count(*) FROM ... GROUP BY field`\n",
    "\n",
    "The conditional logic `if \"count\" in requested_fields` must exist in some form in the code, because the alternatives are over-fetching or under-fetching.  Both of which are far more inefficient in this scenario than in the \"select N+1\" problem.  A dataloader-esque approach is not going to be applicable to \"group by\" operations."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Computation\n",
    "One last generalized example: computed fields.  What are typically query flags in REST (and [RPC](https://en.wikipedia.org/wiki/Remote_procedure_call)) APIs, become field selections in GraphQL.\n",
    "\n",
    "For example, computing scores in a search engine interface.  Instead of a `scores: Boolean! = false` input option, the more obvious approach would be to skip score calculation when the `score` field isn't requested.\n",
    "\n",
    "As with aggregation, the same pattern recurs.  It's unacceptable to over-fetch, i.e., compute the scores when not needed.  It's worse still to under-fetch, i.e., run some sort of lean search that will find matches and then go back and compute scores later."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Solution\n",
    "The inescapable conclusion is that in some cases parent resolvers need to know which of their children are being requested.  There's no need to throw away GraphQL's server-side resolver hierarchy.  No one is advocating a thousand line root resolver named `RunIt` that processes the entire query.\n",
    "\n",
    "All that's needed is a conceptual shift which encourages introspecting the `GraphQLResolveInfo` object.  The requested fields are right there waiting to be useful, but good luck finding documentation and examples to that effect.  In every non-trivial GraphQL project, this author has used a utility like:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def selections(node):\n",
    "    \"\"\"Return tree of field name selections.\"\"\"\n",
    "    nodes = getattr(node.selection_set, 'selections', [])\n",
    "    return {node.name.value: selections(node) for node in nodes}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Those fields would be checked or passed to a data layer as needed.  For example, in [Django's](https://www.djangoproject.com) ORM, it could be as simple as appending a query set with `.values(*selections(*info.nodes))`.\n",
    "\n",
    "Well, almost.  The next issue is that typical GraphQL model validators raise an error if required fields are missing.  Thanks validator; the field is missing because the client didn't request it.\n",
    "\n",
    "This is actually a different age-old problem: equivocating \"optional\" and \"nullable\".  GraphQL requires populating all requested fields, and specifiying whether they may be null.  Server-side implementations understandably, but still incorrectly, interpret that by making nullables optional and non-nullables required at the model layer.  So typically it's necessary to pad resolved objects with empty (but not null) data.\n",
    "\n",
    "Although a minor problem, it reveals the bias related to single-purpose resolvers.  The point of GraphQL is to efficiently return only the requested fields, yet standard practice in GraphQL models is to require populating fields that haven't been requested.\n",
    "\n",
    "Over- and under- fetching can be addressed directly in resolvers, with the data layer's own interface, instead of hidden behind another abstraction layer."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "nikola": {
   "category": "style",
   "date": "2020-07-06",
   "description": "",
   "link": "",
   "slug": "graphql-orm",
   "tags": "",
   "title": "GraphQL - ORM",
   "type": "text"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}